﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;
using System.Configuration;
using System.Diagnostics;
using Common.Logging;

namespace Wicresoft.RDW.Service
{
    public class ExecFeedbackReminder : IJob
    {
        private static ILog log = LogManager.GetLogger(typeof(ExecFeedbackReminder));
       
        public void Execute(JobExecutionContext context)
        {
            log.Info("  start send mail job  for reminder--ExecFeedbackReminder.cs");
            var dns = ConfigurationManager.AppSettings["DNS"];
            var port = int.Parse(ConfigurationManager.AppSettings["RequestPort"]);
            
            ExecSendMail(dns, port,context);
            log.Info("  execute send mail job  for reminder ok--ExecFeedbackReminder.cs");
        }
        /// <summary>
        /// 执行反馈邮件提示
        /// 如果在反馈期间，已添加反馈则取消邮件提示
        /// </summary>
        /// <param name="requestId"></param>
        /// <param name="dns"></param>
        /// <param name="port"></param>
        /// <param name="context"></param>
        private void ExecSendMail( string dns, int port, JobExecutionContext context)
        {
           
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            var requestId = dataMap.GetInt(context.JobDetail.Name);
            var repeatCount = dataMap.GetInt("RepeatCount");
            var feedback = FeedbackPresent.GetByCondition(x => x.RequestId == requestId);
            log.Info(context.JobDetail.Name+" execute times:" + ((SimpleTrigger)(context.Trigger)).TimesTriggered + " at " + DateTime.UtcNow);
            if (feedback != null && feedback.Status == (int)FeedbackStatus.Completed)
            {
                context.Scheduler.DeleteJob(context.JobDetail.Name,"group");
                FeedBackJobList.Jobs.Remove(context.JobDetail.Name);
                log.Info(context.JobDetail.Name+" has added feedback!");
                return;

            }

            var userNameCN = string.Empty;
            var userNameEN = string.Empty;
            var linkAddress = string.Empty;
            var linkEmail = string.Empty;
            var linkPhone = string.Empty;
            var requst = RequestPresent.GetById(requestId);
            var requstLab = LabPresent.GetById(requst.LabId);
            var requestUserEmail = RequestPresent.GetRequestUserEmail(requst.RequestUserId);
            var visitRequest = VisitRequestPresent.GetByCondition(x => x.RequestId == requestId);
            RequestPresent.GetUserNameById(requst.RequestUserId, out userNameCN, out userNameEN);
            linkAddress = "http://" + dns + ":" + port + "/Feedback/Send.aspx?id=" + visitRequest.VisitRequestId + "&language=" + Language.EN.ToString() + "";
            RequestPresent.GetEmailPhoneById(requestId, out linkEmail, out linkPhone);
            var content = new StringBuilder(userNameEN + "^" + linkAddress + "^" + linkEmail + "^" + linkPhone + "^");
            linkAddress = "http://" + dns + ":" + port + "/Feedback/Send.aspx?id=" + visitRequest.VisitRequestId + "&language=" + Language.CN.ToString();
            content.Append(userNameCN + "^" + linkAddress + "^" + linkEmail + "^" + linkPhone);
            MailPresent.SendMail(EmailTemplate.CompleteFeedbackSurveyReminder, content.ToString(), requestUserEmail, new List<string>());

            log.Info("FinalFireTimeUtc:" + context.Trigger.FinalFireTimeUtc + " DateTime.UtcNow" + DateTime.UtcNow);
            if (feedback != null && ((SimpleTrigger)(context.Trigger)).TimesTriggered == repeatCount)
            {
                feedback.Status = (int)FeedbackStatus.Overtime;
                FeedbackPresent.Update(feedback);
                context.Scheduler.DeleteJob(context.JobDetail.Name, "group");
                FeedBackJobList.Jobs.Remove(context.JobDetail.Name);
                log.Info("update feedback to overtime .Last times :" + ((SimpleTrigger)(context.Trigger)).TimesTriggered + " at " + DateTime.UtcNow);
            }
             
        }
    }
}
